<template>
  <div class="add-enterprise">
    <header class="add-header">
      <div class="left">
        <span class="arrow" @click="$router.back()"><i class="el-icon-arrow-left" />返回</span>
        <span>|</span>
        <span>上架商品</span>
      </div>
      <div class="right">
        商品管理
      </div>
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">商品信息</div>
        <div class="form">
          <el-form ref="ruleForm" label-width="100px">
            <el-form-item label="商品名称" prop="item_name">
              <el-input v-model="item.item_name" />
            </el-form-item>
            <el-form-item label="商品价格" prop="price">
              <el-input v-model="item.price" />
            </el-form-item>
            <el-form-item label="商品库存" prop="quantity">
              <el-input v-model="item.quantity" />
            </el-form-item>
            <el-form-item label="商品图片" prop="image">
              <!-- 图片上传    :http-request上传行为   checkUpload检查上传文件是否符合规则-->
              <!-- <el-upload
                action="#"
                :http-request="uploadRequest"
                :before-upload="checkUpload"
            >
            <el-button type="primary">点击上传</el-button>
            </el-upload> -->
              图片：<input type="file" name="image" @change="handleFileChange"><br>

            </el-form-item>
            <el-form-item label="商品描述" prop="description">
              <el-input v-model="item.description" type="textarea" :autosize="{ minRows: 8, maxRows: 16}" />
            </el-form-item>
          </el-form>
        </div>
      </div>
    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button>重置</el-button>
        <el-button type="primary" @click="addItem">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { addItemAPI, upLoadAPI } from '@/api/item'
export default {
  data() {
    return {
      item: {
        item_name: '', // 企业名称
        description: '', // 描述
        price: '',
        quantity: '', // 数量
        image: ''
        // seller_id: ''
      },
      file: ''
    }
  },

  methods: {
    addItem() {
      console.log(this.item.image)
      addItemAPI(this.item).then(() => {
        this.$message.success('添加成功')
      })
    },

    handleFileChange(event) {
      this.file = event.target.files[0]
      const formData = new FormData()
      formData.append('file', this.file)
      upLoadAPI(formData).then((res) => {
        this.item.image = res.msg
        this.$message.success('文件上传成功')
      })
    },

    // 检查上传的文件
    checkUpload(file) {
      // 要求图片大小 小于10M
      return file.size / 1024 / 1024 < 10
    }
  }
}
</script>

<style scoped lang="scss">
.add-enterprise {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    justify-content: space-between;
    padding: 0 20px;
    height: 64px;
    line-height: 64px;
    font-size: 16px;
    font-weight: bold;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }
      .arrow{
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;
      .title {
        height: 60px;
        line-height: 60px;
        padding-left:20px;
      }
      .form {
        margin-bottom: 20px;
        padding: 20px 65px 24px;
        .el-form{
          display: flex;
          flex-wrap: wrap;
          .el-form-item{
            width: 50%;
          }
        }
      }
    }
  }

  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 24px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
  }
}
</style>
